﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyGUI.Properties;
using MyGUI.WcfFlightRoutingService;

namespace MyGUI
{
    public partial class SearchFlights : Form
    {
        private WcfFlightRoutingService.IService wcfService;

        public SearchFlights()
        {
            wcfService = new WcfFlightRoutingService.ServiceClient();
            InitializeComponent();
            setAirportFrom();
            setAirportTo();
            passengerBox.SelectedIndex = 0;
            deviationComboBox.SelectedIndex = 0;
        }


        public void setAirportFrom()
        {
            List<Airport> AirportNames = wcfService.GetAllAirports();
            foreach(Airport i in AirportNames)
            {
                flyFromBox.Items.Add(i.Name);
            }
        }

        public void setAirportTo()
        {
            List<Airport> AirportNames = wcfService.GetAllAirports();

            foreach (Airport i in AirportNames)
            {
                flyToBox.Items.Add(i.Name);
            }
        }

        private Airport flyFromComboBox()
        {
            List<Airport> airportArr = wcfService.GetAllAirports();
            Airport sourceAirportObj = new Airport();
            for (int i = 0; i < flyFromBox.Items.Count - 1; i++)
            {
                if (flyFromBox.SelectedIndex == i)
                {
                    sourceAirportObj = airportArr.ElementAt(i);
                    //MessageBox.Show(airportObj.IdAirport.ToString());
                }
            }
            return sourceAirportObj;
        }

        private Airport flyToComboBox()
        {
            List<Airport> airportArr = wcfService.GetAllAirports();

            Airport destinationAirportObj = new Airport();
            for (int i = 0; i < flyToBox.Items.Count - 1; i++)
            {
                if (flyToBox.SelectedIndex == i)
                {
                    destinationAirportObj = airportArr.ElementAt(i);
                }
            }
            return destinationAirportObj;
        }

        private void allFlightsButton_Click(object sender, EventArgs e)
        {
            AllFlights allFlightsObj = new AllFlights();
            allFlightsObj.Owner = this;
            allFlightsObj.Show();
        }

        #region SearchButton
        private void searchButton_Click(object sender, EventArgs e)
        {
            Airport source = flyFromComboBox();
            //MessageBox.Show(source.Name);
            
            Airport target = flyToComboBox();
            //MessageBox.Show(target.Name);

            string departureDate = departureDatePicker.Value.ToString();
            //MessageBox.Show(departureDate);

            int numPassengers = int.Parse(passengerBox.Text);
            //MessageBox.Show(numPassengers.ToString());

            int deviationDays = int.Parse(deviationComboBox.Text);

            #region wClause
            string wClause = "INNER JOIN Airport ON Flight.idstartairport = Airport.idairport" +
                " WHERE Flight.availableseats - " + numPassengers + " > -6";
            //MessageBox.Show(wClause);
            #endregion
            #region Testmethod for GetCriteriaFlight
            //List<Flight> edgeObjList = wcfService.GetCriteriaFlights(wClause, true);
            //foreach (Flight f in edgeObjList)
            //{
            //    MessageBox.Show("Flight ID: " + f.Idflight);
            //}
            #endregion
            
            List<Airport> dijkstraList = wcfService.RunDijkstra(source, target, wClause, deviationDays);
            //List<Airport> dijkstraList = wcfService.RunBllDijkstra(source, target, wClause);

            #region Test code for printing Dijstra result
            foreach (Airport a in dijkstraList)
            {
                if(a.IdAirport == source.IdAirport)
                {
                    Airport nullAirport = new Airport();
                    nullAirport.Name = "null";
                    a.PreviousVertex = nullAirport;
                    Flight nullFlight = new Flight();
                    nullFlight.StartAirport = a;

                    MessageBox.Show("Vertex: " + a.Name.ToString() + "\n" + "Dist: " + a.Dist.ToString() + "\n" +
                    "Airport ID: " + a.IdAirport.ToString() + "\n" + "Previous vertex: " + a.PreviousVertex.Name + "\n" +
                    "Flight departureAirport: " + a.AdjFlight.StartAirport.Name + "\n" +
                    "Flight destinationAirport " + a.AdjFlight.DestinationAirport.Name + "\n" +
                    "Flight price " + a.Price);
                }
                else
                {
                    MessageBox.Show("Vertex: " + a.Name.ToString() + "\n" + "Dist: " + a.Dist.ToString() + "\n" +
                        "Airport ID: " + a.IdAirport.ToString() + "\n" + "Previous vertex: " + a.PreviousVertex.Name + "\n" +
                        "Flight id: " + a.AdjFlight.Idflight + "\n" + "Flight price " + a.Price);
                }
            }
            #endregion

            ShowTravel showTravelObj = new ShowTravel(dijkstraList);
            showTravelObj.Owner = this;
            showTravelObj.Show();
        }
        #endregion

        #region Unused SelectedIndexChanged Methods
        private void flyFromBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void flyToBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        #endregion
    }
}
